package com.ysstech.reportworld.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.reportworld.entity.Compareconfig;
import com.ysstech.reportworld.service.CompareconfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;

/**
 * 报表世界升级工具数据对比策略信息
 */
@Controller
@Slf4j
@Api(tags = "报表世界据对比策略信息管理api")
@RequestMapping("/compareconfig")
public class CompareconfigController extends BaseController {

    @Autowired
    private CompareconfigService compareconfigService;

    /**
     * 获取一个据对比策略信息的详细信息
     *
     * @return
     * @throws Exception
     */
    @YssPermissions(value = {"reportworld:database:compareconfig"})
    @ApiOperation(value = "获取据对比策略信息信息")
    @YssLog(module = "据对比策略信息管理", remark = "获取据对比策略信息信息")
    @RequestMapping(value = "/queryCompareconfig")
    @ResponseBody
    public RestResult queryCompareconfig() throws Exception {
        QueryWrapper<Compareconfig> compareconfigQueryWrapper = new QueryWrapper<>();
        Compareconfig compareconfig = compareconfigService.getOne(compareconfigQueryWrapper);
        return ResultGenerator.getSuccessResult("获取据对比策略信息信息成功！", compareconfig);
    }

    /**
     * 修改据对比策略信息信息
     *
     * @param compareconfig
     * @return
     */
    @YssPermissions(value = {"reportworld:database:compareconfig"})
    @ApiOperation(value = "修改据对比策略信息信息")
    @YssLog(module = "据对比策略信息管理", remark = "修改据对比策略信息信息")
    @RequestMapping(value = "/edit")
    @ResponseBody
    public RestResult edit(Compareconfig compareconfig) throws Exception {
        if (compareconfig == null) {
            throw new BusinessException("获取据对比策略信息信息失败，请稍后重试！");
        }
        if (StringUtils.isBlank(compareconfig.getId())) {
            compareconfig.setAddUser(redisCacheManager.getLoginUserInfo().getLoginId());
            compareconfig.setAddTime(LocalDateTime.now());
            compareconfig.setId(DateUtil.generateUUID());
            compareconfigService.save(compareconfig);
        } else {
            compareconfig.setEditUser(redisCacheManager.getLoginUserInfo().getLoginId());
            compareconfig.setEditTime(LocalDateTime.now());
            compareconfigService.updateById(compareconfig);
        }
        return ResultGenerator.getSuccessResult("修改据对比策略信息信息成功！");
    }
}
